dojo.provide("ctrlspace.ui.menus.Menu");
dojo.require("dijit.Menu");
dojo.require("dijit.MenuItem");
dojo.require("dijit.MenuSeparator");
dojo.require("dijit.form.DropDownButton");
dojo.declare("ctrlspace.ui.menus.Menu", ctrlspace.core.Plugin, {
	// Default attributes
	id: "ctrlspace.ui.menus.DefaultMenu",
	label: "DefaultMenu",
	location: "default",

	// Constructor
	constructor: function(properties) {
		dojo.safeMixin(this, properties);

		// Initial menu has no items and no positions
		this.menuItems = {};

		// Create menu
		var menu = new dijit.Menu({
			id: this.id,
			style: 'display: none'
		});
		this.menu = menu;

		// Our menu is actually a drop down button tied to a menu
		var button = new dijit.form.DropDownButton({
			id: this.id + '_Actual',
			label: this.label,
			dropDown: this.menu,
			iconClass: this.iconClass == null ? "" : this.iconClass
		});
		this.actualMenu = button;

		// Create location separators
		if (this.locations != null) {
			for (var i in this.locations) {
				var locationId = this.id + '?' + this.locations[i];
				this._createLocation(locationId);
			}
		}
		
		// Tell our MenuBar that a new Menu was created
		IDE.UI.getMenuBar().extend(this.id, this);
	},
	
	_createLocation: function(id) {
		var separator = new dijit.MenuSeparator({id: id});
		this.menu.addChild(separator);
	},
	
	extend: function(id, menuItem) {
		var enabled = true;
		var click = null;
		if (menuItem.command == null) {
			enabled = false;
		} else {
			click = IDE._pluginRegistry[menuItem.command][0];
		}
		
		var dijitMenuItem = new dijit.MenuItem({
			id: menuItem.id,
			label: menuItem.label,
            iconClass: menuItem.iconClass == null ? "" : menuItem.iconClass,
            onClick: function() {
				click.execute();
			}
		});
		
		var position = menuItem.position;
		var index = this.menu.getIndexOfChild(dijit.byId(position));
		
		if (index == -1) {
			this.menu.addChild(dijitMenuItem);
		} else {
			this.menu.addChild(dijitMenuItem, index);
		}
		this.menuItems[id] = menuItem;
	}
});